<?php
/**
 * 
 * @author tangbl
 * 
 */
class DictAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index(){
		
		$lang = getLang();
		if(IS_AJAX){
			
			$key  = $this->_param("key");
			$list = F("Dict_".$key);

			$modi = L("dit_edit_page_title");
			$econ = L('dit_form_edit_cont');
			$acon = L('dit_form_add_cont');
			$add  = L('dit_form_add_child');
			foreach($list as $k => $r) {
				
				$uetid     = U('/Dict/edit/','id='.$r['id']);
				$r['edit'] = '<a href="'.$uetid.'" title="'.$modi.'" class="btn blue"><i class="icon-edit"></i></a>';
				
				$r['add']  = '<a href="'.U('Dict/add/','id='.$r['id']).'" class="btn green"><i class="icon-plus" title="'.$add.'"></i></a>';
				if($r['type'] == 1){
					if($r['level']>=2){
						$r['add']  = '<a href="'.U('Dict/add/','id='.$r['id']).'" class="btn green"><i class="icon-plus" title="'.$add.'"></i></a>';
						//$r['add']  = '<a href="#" class="btn"><i class="icon-plus" title="'.$add.'"></i></a>';
					}
				}
			
				if($r['haveLeaf'] == 1){
					$r['del'] = '<a href="#" class="btn"  data-id="'.$r['id'].'"><i class="icon-remove"></i></a>';
				}else{
					$r['del'] = '<a href="#" class="btn red delAlone"  data-id="'.$r['id'].'"><i class="icon-remove"></i></a>';
				}
				//处理名称
				$r['title']  = dejsonByLang($r['value'],$lang);
				
				$r['levels'] = $r['level'];
				$array[]     = $r;
			}
			
			$str  = "<tr class='tr level_\$levels' leaf='\$haveLeaf' level='\$levels'>
				    <td align='center'><input style='width:55px' type='text' value='\$sort' size='3' name='sort[\$id]'></td>
				    <td class='name'>\$spacer \$title</td>
				    <td align='center'>\$sort</td>
					<td align='center'>
						\$add \$edit \$del
					</td></tr>";
			Vendor('Common.Tree');
			$Tree = new Tree();
			$Tree->icon = array('　│　','　├─ ','　└─ ');
			$Tree->nbsp = '　';
			$Tree->init($array);
			$html_tree = $Tree->get_tree(0, $str);
			$this->ajaxReturn(1,$html_tree,1);
			
		}else{
			
			$model = D("Dict");
			$list = $model->getTopDict();
			
			$key  = $this->_param("key","","");
			$rows = array();
			foreach($list as $k=>$v){
				$flag  = ($v['key'] == $key) ? "selected" : '' ;
				$title = dejsonByLang($v['value'],$lang);
				$rows[$v['key']] = array("title"=>$title,"checked"=>$flag);
			}
			$this->assign("keys",$rows);
			$this->display();
		}
		
	}
	
	/**
	 * 添加
	 */
	public function add(){
		
		$data =  D("Dict");
		$lang = getLang();
		
		if(IS_POST){
			
			if($data->create()){
				
				$pid    = $this->_post("pid","intval",0);
				$key    = $this->_post("key","","");
				$type   = $this->_post("type","","");
				$sort   = $this->_post("sort","","");
				$time   = time();
				$values = $this->_post("value");
				
				if($pid){
					$multi  = $this->_post("multi","intval",0);
					
					if($multi){
						$lanArr = C("LANG_ARRAY");
						$base   = array();
						$cArr   = 0;
						foreach($lanArr as $k=>$v){
							$tarray   = explode("\r\n",trim($values[$k])) ;
							$base[$k] = $tarray ;
							
							if(count($tarray)>$cArr){
								$cArr = count($tarray);
							}
						}
						$id = array();
						for($i=0 ; $i<$cArr ; $i++){
							$val = array();
							foreach($lanArr as $k=>$v){
								$val[$k] = $base[$k][$i] ;
							}
							$save['value']   = json_encode($val);
							$save['pid']     = $pid ;
							$save['type']    = $type ;
							$save['key']     = $key ;
							$save['sort']    = $sort ;
							$save['solevar'] = create_id() ;
							$save['addtime'] = $time ;
							$id[] = $data->data($save)->add();
						}
					}else{
						$data->value = json_encode($values);
						$id   = $data->add();
					}
				}else{
					$validate = array(
						array('key','','字典标识不能重复',1,'unique',1)
					);
					$data-> setProperty("_validate",$validate);
					$data->value = json_encode($values);
					$id   = $data->add();
				}
				
				$data->genDict($pid,$id,$key);
				wlog(1);
				
				$this->assign("jumpUrl",U('/Dict/index',"key=".$key));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(1,$data->getError());
				$this->error($data->getError());
			}
		}else{
			
			$id = $this->_param("id","intval",0);
			if($id){
				$row   = $data->find($id);
				$title = dejsonByLang($row['value'],$lang);
				$this->assign("title",$title);
				$this->assign("row",$row);
			}
			$this->assign("lang",C("LANG_ARRAY"));
			
			$type  = "select";
			$dict  = C("DICT_CATEGORY");
			$cate  = array();
			foreach($dict as $k=>$v){
				$flag = ($k == $type) ? "checked" : "" ;
				$cate[$k] = array('v'=>$v,'checked'=>$flag);
			}
			$this->assign("module",$cate);
			$this->display();
		}
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		$data  = D("Dict");
		$lang  = getLang();
		$id    = $this->_param("id","intval",0);
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		if(IS_POST){
			if($data->create()){
				
				$values = $this->_post("value");
				$pid    = $this->_post("pid","intval",0);
				$key    = $this->_post("key","","");
				$data->value = json_encode($values);
				$data->save();
				
				$data->genDict($pid,$id,$key);
				wlog(1);
			
				$this->assign("jumpUrl",U('Dict/index',"key=".$key));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(1,$data->getError());
				$this->error($data->getError());
			}
		}else{
			
			$row = $data->find($id);
			$this->assign("row",$row);
			
			if($row['pid']){
				$prow  = $data->find($row['pid']);
				$title = dejsonByLang($prow['value'],$lang);
				$this->assign("title",$title);
			}
			
			$value = json_decode($row['value'],true);
			$lang  = C("LANG_ARRAY");
			$vals  = array();
			foreach($lang as $k=>$v){
				$title = $value[$k];
				$vals[$k] = array('v'=>$v,'title'=>$title);
			}
			$this->assign("lang",$vals);
			
			$type  = $row['type'];
			$dict  = C("DICT_CATEGORY");
			$cate  = array();
			foreach($dict as $k=>$v){
				$flag = ($k == $type) ? "checked" : "" ;
				$cate[$k] = array('v'=>$v,'checked'=>$flag);
			}
			$this->assign("module",$cate);
			
			$this->display();
		}
	}
	
	/**
	 * 生成字典缓存
	 */
	public function cache(){
	
		//重新生成字典缓存
		$dict = D("Dict");
		$dict->cacheDict();
		wlog(1);
		$this->success("生成字典缓存成功");
	}

	/**
	 * 删除
	 */
	public function del(){
		
		 $model  = D("Dict");
		 $id     = $this->_get('id',"intval","0");
		 $rec    = $model->find($id);
		 $res1   = $model->delete($id);
		 
		 $model->updLeaf($rec["pid"],true);
		 $model->updTotal($rec['key']);
		 $model->getDictByKey($rec['key']);
		 
		 if($res1){
		 	wlog(1);
			$this->ajaxReturn($res1,L('cfg_form_tipmsg_delsucc'),1);
		 }else{
			 wlog(2);
			 $this->ajaxReturn(0,L('cfg_form_tipmsg_delfail'),0);
		 }
	}
	
	/**
	 * 栏目排序
	 */
	function sort(){
		$sorts = $this->_post('sort');
		$key   = $this->_post('key');
		$model = D("Dict");
		if(!is_array($sorts))$this->error(L('cfg_form_tipmsg_illgel'));
		foreach ($sorts as $id => $sort) {
			$model->save(array("id"=>$id,"sort"=>$sort));
		}
		$model->getDictByKey($key);
		/*
		$model->getDictByKey("material");
		$model->getDictByKey("address");
		$model->getDictByKey("size");
		$model->getDictByKey("color"); */
		$this->assign("jumpUrl",U('/Dict/index','key='.$key));
		$this->success(L('dit_oth_sort_succ'));
	}
	
}

?>